home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / schematk / src_1152.lha / Agent.subproj / HelpAgent.m < prev    next >
Encoding:
Text File  |  1991-12-15  |  9.5 KB  |  354 lines

  1. /* Copyright รก 1991 Gustavus Adolphus College.  All rights reserved.
  2.  *
  3.  * Schematik was developed by Gustavus Adolphus College (GAC) with
  4.  * support from NeXT Computer, Inc.  Permission to copy this software,
  5.  * to redistribute it, and to use it for any purpose is granted,
  6.  * subject to the following restrictions and understandings.
  7.  *
  8.  * 1. Any copy made of this software must include this copyright
  9.  * notice in full.
  10.  *
  11.  * 2. Users of this software agree to make their best efforts (a) to
  12.  * return to the GAC Mathematics and Computer Science Department any
  13.  * improvements or extensions that they make, so that these may be
  14.  * included in future releases; and (b) to inform GAC of noteworthy
  15.  * uses of this software.
  16.  *
  17.  * 3. All materials developed as a consequence of the use of this
  18.  * software shall duly acknowledge such use, in accordance with the
  19.  * usual standards of acknowledging credit in academic research.
  20.  *
  21.  * 4. GAC makes no express or implied warranty or representation of
  22.  * any kind with respect to this software, including any warranty
  23.  * that the operation of this software will be error-free.  ANY
  24.  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
  25.  * PURPOSE IS HEREBY DISCLAIMED.  GAC is under no obligation to
  26.  * provide any services, by way of maintenance, update, or otherwise.
  27.  *
  28.  * 5. In conjunction with products arising from the use of this
  29.  * material, there shall be no use of the name of Gustavus Adolphus
  30.  * College nor of any adaptation thereof in any advertising,
  31.  * promotional, or sales literature without prior written consent
  32.  * from GAC in each case.
  33.  */
  34.  
  35. #import "HelpAgent.h"
  36. #import "../defines.h"
  37. #import Main_h
  38. #import PrefAgent_h
  39. #import <appkit/NXBrowser.h>
  40. #import <appkit/NXBrowserCell.h>
  41. #import <appkit/ScrollView.h>
  42. #import <appkit/Text.h>
  43. #import <sys/dir.h>
  44. #import <fcntl.h>
  45. #import <libc.h>
  46. #import <mach.h>
  47. #import <string.h>
  48.  
  49. #define NIBFILE1        "Help.nib"
  50. #define NIBFILE2        "Manual.nib"
  51. #define HELPZONENAME        "HelpAgnt"
  52. #define MINHELPWINDOWHEIGHT    95
  53. #define MINHELPWINDOWWIDTH    400
  54. #define MINMANWINDOWHEIGHT    130
  55. #define MINMANWINDOWWIDTH    550
  56.  
  57. #define MANUALPANELTITLE    LocalString("Scheme 7.1 Reference Manual",NULL)
  58. #define MANUALBROWSERTITLE    LocalString("Page",NULL)
  59. #define ENTRYFORMTITLE        LocalString("Search for Entry:",NULL)
  60. #define SEARCHBUTTONTITLE    LocalString("Search",NULL)
  61.  
  62. static char **addFile(const char *, char **, int);
  63. static int compare(void *, void *);
  64.  
  65. static NXZone *helpZone=NULL;
  66. static char **topics=NULL;
  67. static id theAgent=nil;
  68.  
  69. @implementation HelpAgent
  70.  
  71. + new
  72. {
  73. DEBUG_FUNC1(DEBUGLEVEL);
  74.     if (theAgent==nil)
  75.       {
  76.         NXZone *tempZone;
  77.  
  78.         tempZone = NXCreateZone(vm_page_size, vm_page_size, YES);
  79.         DEBUG_ASSERT(tempZone!=NULL)
  80.         theAgent = self = [super allocFromZone:tempZone];
  81.     helpZone = agentZone = tempZone;
  82.     NXNameZone(agentZone, HELPZONENAME);
  83.       }
  84.     return theAgent;
  85. }
  86.  
  87. - activateAgent:sender
  88. {
  89. DEBUG_FUNC1(DEBUGLEVEL);
  90.     switch ([sender tag])
  91.       {
  92.         case MENU_INFO_Help  : if (!helpPanel)
  93.                  {
  94.                    helpPanel = LoadNIB(NIBFILE1, theAgent, agentZone);
  95.                    DEBUG_ASSERT(helpPanel!=nil)
  96.                    [helpPanel useOptimizedDrawing:YES];
  97.                    [helpPanel setTitle:HELPPANELTITLE];
  98.                    [topicsBrowser setTitle:HELPBROWSERTITLE ofColumn:BROWSERCOLUMN];
  99.                    [topicsBrowser loadColumnZero];
  100.                    DEBUG_ASSERT([topicsBrowser isLoaded])
  101.                  }
  102.                    [helpPanel makeKeyAndOrderFront:self];
  103.                    break;
  104.         case MENU_INFO_Manual: if (!manualPanel)
  105.                  {
  106.                    manualPanel = LoadNIB(NIBFILE2, theAgent, agentZone);
  107.                    DEBUG_ASSERT(manualPanel!=nil)
  108.                    [manualPanel useOptimizedDrawing:YES];
  109. #if 0
  110.                    [manualPanel setTitle:MANUALPANELTITLE];
  111.                    [pageBrowser setTitle:MANUALBROWSERTITLE ofColumn:BROWSERCOLUMN];
  112.                    [pageBrowser setDoubleAction:@selector(manualBrowserDoubleClick:)];
  113.                    [entryField setTitle:ENTRYFORMTITLE at:0];
  114.                    [[entryField target] setTitle:SEARCHBUTTONTITLE];
  115.                    [pageBrowser loadColumnZero];
  116.                    DEBUG_ASSERT([pageBrowser isLoaded])
  117. #endif
  118.                  }
  119.                    [manualPanel makeKeyAndOrderFront:self];
  120.       }
  121.     return self;
  122. }
  123.  
  124. - helpBrowserClick:sender
  125. {
  126.     extern char *appDirectory;
  127.     const char *topic=[[[sender matrixInColumn:BROWSERCOLUMN] selectedCell] stringValue];
  128.  
  129. DEBUG_FUNC1(DEBUGLEVEL);
  130.     if ((topic!=NULL)&&(*topic))
  131.       {
  132.         NXStream *stream;
  133.         char helpFile[strlen(appDirectory)+strlen(topic)+strlen(FILENAMETEMPLATE)+strlen(NOHELPFILE)];
  134.  
  135.         sprintf(helpFile, FILENAMETEMPLATE, appDirectory, topic);
  136.         if (!(stream = NXMapFile(helpFile, NX_READONLY)))
  137.           {
  138.             sprintf(helpFile, FILENAMETEMPLATE, appDirectory, NOHELPFILE);
  139.             stream = NXMapFile(helpFile, NX_READONLY);
  140.           }
  141.         if (stream)
  142.           {
  143.             [helpPanel disableFlushWindow];
  144.             [[helpScrollView docView] readRichText:stream];
  145.         [[helpScrollView docView] scrollSelToVisible];
  146.             [[helpPanel reenableFlushWindow] flushWindow];
  147.             NXCloseMemory(stream, NX_FREEBUFFER);
  148.           }
  149.       }
  150.     return self;
  151. }
  152.  
  153. #if 0
  154. - manualBrowserClick:sender
  155. {
  156.     return self;
  157. }
  158.  
  159. - manualBrowserDoubleClick:sender;
  160. {
  161.     const char *label=[[[sender matrixInColumn:BROWSERCOLUMN] selectedCell] stringValue];
  162.     return self;
  163. }
  164. #endif
  165.  
  166. - (id)helpScrollView    { return helpScrollView;}
  167.  
  168. @end
  169.  
  170. @implementation HelpAgent (HelpBrowserDelegate)
  171.  
  172. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  173. {
  174.     extern char *appDirectory;
  175.     char helpDirectory[strlen(appDirectory)+strlen(HELPDIR)];
  176.     int fd,count=0;
  177.     char **list=NULL;
  178.  
  179. DEBUG_FUNC1(DEBUGLEVEL);
  180.     sprintf(helpDirectory, HELPDIR, appDirectory);
  181.     if ((fd = open(helpDirectory, O_RDONLY, 0644))>0)
  182.       {
  183.         long basep;
  184.         char dirbuf[vm_page_size],*buf;
  185.         struct direct *dp;
  186.         int cc=getdirentries(fd, (buf = dirbuf), vm_page_size, &basep);
  187.  
  188.         while (cc)
  189.           {
  190.             dp = (struct direct *)buf;
  191.             if ((dp->d_name[0])&&(dp->d_name[0]!='.'))
  192.               list = addFile(dp->d_name, list, count++);
  193.             buf += dp->d_reclen;
  194.             if (buf>=dirbuf+cc)
  195.               cc = getdirentries(fd, (buf = dirbuf), vm_page_size, &basep);
  196.           }
  197.         close(fd);
  198.         if (list)
  199.           qsort(list, count, sizeof(char *), compare);
  200.       }
  201.     if (topics)
  202.       {
  203.         int i;
  204.         for(i=0; topics[i]; i++)
  205.           NXZoneFree(agentZone, topics[i]);
  206.         NXZoneFree(agentZone, topics);
  207.       }
  208.     topics = list;
  209.     return count;
  210. }
  211.  
  212. - browser:sender loadCell:aCell atRow:(int)row inColumn:(int)column
  213. {
  214. DEBUG_FUNC1(DEBUGLEVEL);
  215.     if (topics)
  216.       {
  217.         [aCell setStringValueNoCopy:topics[row]];
  218.         DEBUG_ASSERT(!NXOrderStrings((unsigned char *)[aCell stringValue], (unsigned char *)topics[row], YES, -1, NULL))
  219.         [aCell setLeaf:YES];
  220.         [aCell setLoaded:YES];
  221.       }
  222.     return self;
  223. }
  224.  
  225. @end
  226.  
  227. @implementation HelpAgent (Delegate)
  228.  
  229. - windowWillResize:sender toSize:(NXSize *)frameSize
  230. {
  231. DEBUG_FUNC1(DEBUGLEVEL);
  232.     if ([sender isEqual:helpPanel])
  233.       {
  234.     if (frameSize->width<MINHELPWINDOWWIDTH)
  235.       frameSize->width = MINHELPWINDOWWIDTH;
  236.     if (frameSize->height<MINHELPWINDOWHEIGHT)
  237.       frameSize->height = MINHELPWINDOWHEIGHT;
  238.       }
  239.     else if ([sender isEqual:manualPanel])
  240.       {
  241.     if (frameSize->width<MINMANWINDOWWIDTH)
  242.       frameSize->width = MINMANWINDOWWIDTH;
  243.     if (frameSize->height<MINMANWINDOWHEIGHT)
  244.       frameSize->height = MINMANWINDOWHEIGHT;
  245.       }
  246.     return self;
  247. }
  248.  
  249. - windowWillClose:sender
  250. {
  251. DEBUG_FUNC1(DEBUGLEVEL);
  252.     if ([sender isEqual:helpPanel])
  253.       helpPanel = nil;
  254.     else if ([sender isEqual:manualPanel])
  255.       manualPanel = nil;
  256.     return self;
  257. }
  258.  
  259. @end
  260.  
  261. #define CHUNK    3
  262.  
  263. static char **addFile(const char *file, char **list, int count)
  264. {
  265.     char *suffix;
  266.  
  267.     if (!list)
  268.       list = (char **)NXZoneMalloc(helpZone, CHUNK*sizeof(char *));
  269.     DEBUG_ASSERT(list!=NULL)
  270.     if (suffix = rindex(file, '.'))
  271.       *suffix = '\0';
  272.     list[count] = NXCopyStringBufferFromZone(file, helpZone);
  273.     DEBUG_ASSERT(list[count]!=NULL)
  274.     count++;
  275.     if (!(count%CHUNK))
  276.       list = (char **)NXZoneRealloc(helpZone, list, (count+CHUNK)*sizeof(char *));
  277.     DEBUG_ASSERT(list!=NULL)
  278.     list[count] = NULL;
  279.     return list;
  280. }
  281.  
  282. static int compare(void *arg1, void *arg2)
  283. {
  284.     return NXOrderStrings(*((unsigned char **)arg1), *((unsigned char **)arg2), NO, -1, NULL);
  285. }
  286.  
  287. @implementation ManualView
  288.  
  289. - initFrame:(NXRect *)aRect
  290. {
  291.     return self;
  292. }
  293.  
  294. - drawSelf:(const NXRect *)rects :(int)rectCount
  295. {
  296.     return self;
  297. }
  298.  
  299. - displayPage:(unsigned)aPage at:(int)aLocation
  300. {
  301.     return self;
  302. }
  303.  
  304. - generateIndex
  305. {
  306.     return self;
  307. }
  308.  
  309. @end
  310.  
  311. @implementation ManualView (PageBrowserDelegate)
  312. #if 0
  313. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  314. {
  315.     int count;
  316.     id aCell;
  317.  
  318. DEBUG_FUNC1(DEBUGLEVEL);
  319.     for(count=0; count<[entryIndexList count]; count++)
  320.       {
  321.         [matrix addRow];
  322.     aCell = [matrix cellAt:count :0];
  323.     [aCell setStringValue:[[entryIndexList objectAt:count] entry]];
  324.     [aCell setLeaf:YES];
  325.     [aCell setLoaded:YES];
  326.       }
  327.     return count+1;
  328. }
  329. #endif
  330. @end
  331.  
  332. @implementation ManualEntry
  333.  
  334. - init:(char *)anEntry page:(short)aPage line:(int)aLine label:(char *)aLabel x:(short)x y:(short)y
  335. {
  336.     [super init];
  337.     entryName = NXCopyStringBuffer(anEntry);
  338.     absPage = aPage;
  339.     pageStartLine = aLine;
  340.     pageLabel = NXCopyStringBuffer(aLabel);
  341.     xPos = x;
  342.     yPos = y;
  343.     return self;
  344. }
  345.  
  346. - (char *)entry        { return entryName;}
  347. - (char *)pageLabel;    { return pageLabel;}
  348. - (short)absolutePage;    { return absPage;}
  349. - (int)pageStartLine    { return pageStartLine;}
  350. - (short)xPosition;    { return xPos;}
  351. - (short)yPosition;    { return yPos;}
  352.  
  353. @end
  354.